<template>
  <div class="login_container">
    <!-- 登录框 -->
    <div class="login_box">
      <!-- 图片 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>

      <!-- 表单 -->
      <el-form :model="loginForm" :rules="loginFormrules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" clearable v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" clearable v-model="loginForm.password" show-password></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单数据验证规则
      loginFormrules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名在3到10个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 12, message: '密码在6到12个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单事件函数
    resetForm() {
      this.$refs.loginFormRef.resetFields();
    },

    // 登录事件函数
    login() {
      // 表单预验证
      this.$refs.loginFormRef.validate(async valid => {
        // console.log(valid);
        if (!valid) return;
        // 验证通过，发起请求
        const res = await this.$axios.post('login', this.loginForm);
        // console.log(res);
        if (res.meta.status !== 200) return this.$msg.error('登陆失败！');
        // 保存用户名和token到本地存储
        window.sessionStorage.setItem('token', res.data.token);
        window.sessionStorage.setItem('username', res.data.username);
        //  跳转到home页面
        this.$router.push('/home');
      });
    }
  },
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background: url(../assets/21902.jpg) no-repeat center;
  .login_box {
    width: 450px;
    height: 300px;
    position: absolute;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.7);
    .logo {
      width: 130px;
      height: 130px;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      border: 1px solid #212c48;
      box-shadow: 0 0 10px 5px #fff;
      background-color: rgba(0, 0, 0, 0.7);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        box-shadow: 0 0 10px #fff;
      }
    }
    .el-form {
      position: absolute;
      bottom: 20px;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .iconfont {
      margin-left: 5px;
    }
  }
}
</style>